/*
 ============================================================================
 Name		: TerraEngine.cpp
 Author	  : Jan Sanchez
 Version	 : 1.0
 Copyright   : For testing purposes only!
 Description : CTerraEngine implementation
 ============================================================================
 */

#include "TerraEngine.h"
#include <aknquerydialog.h>
#include <aknnotewrappers.h>

#include <TerraWS_0xE563F630.rsg>

CTerraEngine::CTerraEngine()
	{
	// No implementation required
	}

CTerraEngine::~CTerraEngine()
	{
	}

CTerraEngine* CTerraEngine::NewLC()
	{
	CTerraEngine* self = new (ELeave) CTerraEngine();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CTerraEngine* CTerraEngine::NewL()
	{
	CTerraEngine* self = CTerraEngine::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CTerraEngine::ConstructL()
	{	
	}

void CTerraEngine::ConnectL()
	{
	CSenXmlServiceDescription* pServiceDesc = CSenXmlServiceDescription::NewLC(KServiceEndpoint, KNullDesC8());
	pServiceDesc->SetFrameworkIdL(KDefaultBasicWebServicesFrameworkID);
	iObserver = CTerraCallback::NewLC();
	iService = CTerraServiceService::NewLC(*iObserver, *pServiceDesc);

	CActiveScheduler::Start();   // Wait for callback setstatus
	}


void CTerraEngine::GetLocationL()
	{	
	if (!iService)
		{
		this->ConnectL();
		}
	_LIT(KLatTitle,"Insert Latitude");
	_LIT(KLongTitle,"Insert Longitude");
	TInt Lat=0, Long=0;
	this->ShowQueryDialogL(KLatTitle, KLongTitle, Lat, Long);
	
	RConvertLonLatPtToNearestPlace Coordinates;
	RConvertLonLatPtToNearestPlaceResponse Place;
	CSenSoapFault* fault = NULL;
	Coordinates.iPoint.iLat = Lat;
	Coordinates.iPoint.iLon = Long;
	
	if (iObserver->iConnStatus == 1)  // if the status of the callback changes to 1
		{
		iService->ConvertLonLatPtToNearestPlaceL(Coordinates, Place, &fault);	
		this->ShowInfoDialogL(Place.iConvertLonLatPtToNearestPlaceResult);
		}
	else 
		{
		
		this->ShowInfoDialogL(KConnectionError);
		}
	
	}

void CTerraEngine::GetUTML()
	{
	if (!iService)
		{
		this->ConnectL();
		}
	
	_LIT(KLatTitle,"Insert Latitude");
	_LIT(KLongTitle,"Insert Longitude");
	TInt Lat=0, Long=0;
	this->ShowQueryDialogL(KLatTitle, KLongTitle, Lat, Long);
	
	RConvertLonLatPtToUtmPt Coordinates;
	Coordinates.iPoint.iLat = Lat;
	Coordinates.iPoint.iLon = Long;
	RConvertLonLatPtToUtmPtResponse utmPlace;
	CSenSoapFault* fault = NULL;		
	
	if (iObserver->iConnStatus == 1) 
		{
		iService->ConvertLonLatPtToUtmPtL(Coordinates, utmPlace, &fault);
		
		// Formatting UTM data for displaying
		_LIT(KFormat1, "UTM Zone: %d\nX: %.3f\nY: %.3f");
		TBuf<50> str;
		str.Format(KFormat1, utmPlace.iConvertLonLatPtToUtmPtResult.iZone, utmPlace.iConvertLonLatPtToUtmPtResult.iX,
				 utmPlace.iConvertLonLatPtToUtmPtResult.iY);
		
		this->ShowInfoDialogL(str);
		}
	else
		this->ShowInfoDialogL(KConnectionError);
	
	}

void CTerraEngine::GetCoordinatesL()
	{
	if (!iService)
		{
		this->ConnectL();
		}
	
	_LIT(KCity,"Insert a City");
	_LIT(KCountry,"Insert a Country");
	
	TBuf<15> City, Country;
	this->ShowQueryDialogL(KCity, KCountry, City, Country);
	
	RConvertPlaceToLonLatPt Place;
	
	HBufC* CityC;
	CityC = City.Alloc();
	HBufC* CountryC;
	CountryC = Country.Alloc();
	HBufC* StateC;
	StateC = KState().Alloc();

	Place.iPlace.iCity = CityC;
	Place.iPlace.iCountry = CountryC;
	Place.iPlace.iState =  StateC;
	
	RConvertPlaceToLonLatPtResponse Coordinates;
	CSenSoapFault* fault = NULL;	

	if (iObserver->iConnStatus == 1) 
		{
		iService->ConvertPlaceToLonLatPtL(Place, Coordinates, &fault);
	
		// Formatting Coordinates data:
		_LIT(KFormat1, "Latitude: %.3f \nLongitud: %.3f");
		TBuf<50> str;
		str.Format(KFormat1, Coordinates.iConvertPlaceToLonLatPtResult.iLat, Coordinates.iConvertPlaceToLonLatPtResult.iLon);
		this->ShowInfoDialogL(str);
		}
	else
		this->ShowInfoDialogL(KConnectionError);
	
	}

TInt CTerraEngine::ShowQueryDialogL(const TDesC& aTitle1, const TDesC& aTitle2, TInt& aLat, TInt& aLong)
	{
	CAknMultiLineDataQueryDialog* dialog = CAknMultiLineDataQueryDialog::NewL(aLat, aLong, CAknQueryDialog::ENoTone);
	dialog->SetPromptL(aTitle1, aTitle2);
	return dialog->ExecuteLD(R_MULTI_QUERY_NUM);
	}

TInt CTerraEngine::ShowQueryDialogL(const TDesC& aTitle1, const TDesC& aTitle2, TDes& aCity, TDes& aCountry)
	{
	CAknMultiLineDataQueryDialog* dialog = CAknMultiLineDataQueryDialog::NewL(aCity, aCountry, CAknQueryDialog::ENoTone);
	dialog->SetPromptL(aTitle1, aTitle2);
	return dialog->ExecuteLD(R_MULTI_QUERY_STRINGS);
	}

TInt CTerraEngine::ShowInfoDialogL(const TDesC& anInfo)
	{
	CAknInformationNote* informationNote = new ( ELeave ) CAknInformationNote;
	informationNote->SetTimeout(CAknNoteDialog::ENoTimeout);
	return informationNote->ExecuteLD(anInfo);
	}
